package com.erp.bus.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.bus.service.ProviderService;
import com.erp.bus.comm.DataAndView;
import com.erp.bus.comm.ResultCode;
import com.erp.bus.pojo.Provider;
import com.erp.bus.vo.ProviderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping("loadAllProvider")
    public DataAndView loadAllCustomer(ProviderVo providerVo) {

       IPage<Provider> page =   new Page<Provider>(providerVo.getPage(),providerVo.getLimit());

       // 条件查询
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        if(providerVo.getProvidername() != null){
            qw.like("providername",providerVo.getProvidername());
        }
        if(providerVo.getPhone() != null){
            qw.like("telephone",providerVo.getPhone());
        }
        if(providerVo.getConnectionperson() != null){

            qw.like("connectionperson",providerVo.getConnectionperson());
        }

         providerService.page(page,qw);

        DataAndView da =   new DataAndView(page.getTotal(),page.getRecords());

        return da;

    }

    @RequestMapping("addProvider")
    public ResultCode addProvider(Provider provider){
        // 获取用户填写的数据
        ;try{
            provider.setId(null);
            boolean bool = providerService.save(provider);
            if(bool){
                return ResultCode.SUCCESS_ADD;
            }else{
                return ResultCode.FAIL_ADD;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_ADD;
        }


    }

    //修改供应商
    @RequestMapping("updateProvider")
    public ResultCode updateProvider(Provider provider){
        // 获取用户填写的数据
        ;try{
            boolean bool = providerService.updateById(provider);
            if(bool){
                return ResultCode.SUCCESS_UPDATE;
            }else{
                return ResultCode.FAIL_UPDATE;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_UPDATE;
        }
    }

    @RequestMapping("deleteProvider")
    public ResultCode  deleteProvider(ProviderVo  providerVo){

        System.out.println(providerVo.getId());
        ;try{
            System.out.println(providerVo.getId());
            boolean bool = this.providerService.removeById(providerVo.getId());
            System.out.println(bool);
            if(bool){
                return ResultCode.SUCCESS_DELETE;
            }else{
                return ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_DELETE;
        }

    }

    @RequestMapping("batchDeleteProvider")
    public ResultCode batchDeleteProvider(ProviderVo providerVo){

        List<Integer> list = new ArrayList<>();
        for(Integer id :providerVo.getIds()){

            list.add(id);
        }
        try {
            boolean info  =providerService.removeByIds(list);
            if(info){
                return ResultCode.SUCCESS_DELETE;
            }else{
                return  ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){
            return  ResultCode.FAIL_DELETE;

        }

    }

    @RequestMapping("loadAllProviderForSelect")
    public DataAndView loadAllProviderForSelect(){

        List<Provider> list = providerService.list();
        return new DataAndView(list);
    }

}
